package com.gzhuxj201.service.Impl;

import com.gzhuxj201.entity.Class;
import com.gzhuxj201.entity.Student;
import com.gzhuxj201.mapper.ClassMapper;
import com.gzhuxj201.mapper.StudentMapper;
import com.gzhuxj201.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Class> selectAll() {
        return classMapper.selectAll();
    }

    @Transactional(rollbackFor = Exception.class)//通过rollbackFor可以知道什么异常才回滚事务,默认是运行时异常才会回滚事务
    @Override
    public int deleteById(int id) {
        //删除班级
         classMapper.deleteById(id);
        //删除班级下的学生
        return studentMapper.deleteByClassId(id);
    }

    @Override
    public int add(Class cla) {
        return classMapper.add(cla);
    }

    @Override
    public Class selectById(int id) {
        return classMapper.selectById(id);
    }


    @Override
    public int update(Class cla) {
        return classMapper.update(cla);

    }
}
